<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Petugas extends CI_Controller {

    function __construct() { //inisialisasi library dan helper yang perlu di load
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('pagination');
        $this->load->model('simades');
        $log = $this->session->all_userdata();
        if (!isset($log) || $log['login'] != TRUE || $log['privilege'] != 2) {
            redirect('main/logout');
        }
    }

    function index() {
        redirect('petugas/home');
    }

    function home() {
        $data['title'] = 'Home';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('template/alert', $data);
        $this->load->view('petugas/home', $data);
        $this->load->view('template/foot');
    }

    function kk() {
        $data['title'] = 'Kartu keluarga';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['kk'] = $this->simades->get_kk()) {
            $data['kk'] = $this->simades->get_kk();
        } else {
            $data['kk'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/kk', $data);
        $this->load->view('template/foot');
    }

    function kk_tambah() {
        $data['title'] = 'Tambah Kartu Keluarga';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['sex'] = $this->simades->get_sex()) {
            $data['sex'] = $this->simades->get_sex();
        } else {
            $data['sex'] = array();
        }
        if ($data['darah'] = $this->simades->get_darah()) {
            $data['darah'] = $this->simades->get_darah();
        } else {
            $data['darah'] = array();
        }
        if ($data['agama'] = $this->simades->get_agama()) {
            $data['agama'] = $this->simades->get_agama();
        } else {
            $data['agama'] = array();
        }
        if ($data['skul'] = $this->simades->get_pendidikan()) {
            $data['skul'] = $this->simades->get_pendidikan();
        } else {
            $data['skul'] = array();
        }
        if ($data['gawe'] = $this->simades->get_pekerjaan()) {
            $data['gawe'] = $this->simades->get_pekerjaan();
        } else {
            $data['gawe'] = array();
        }
        if ($data['cacat'] = $this->simades->get_cacat()) {
            $data['cacat'] = $this->simades->get_cacat();
        } else {
            $data['cacat'] = array();
        }
        if ($data['perkawinan'] = $this->simades->get_perkawinan()) {
            $data['perkawinan'] = $this->simades->get_perkawinan();
        } else {
            $data['perkawinan'] = array();
        }
        if ($data['hubungan'] = $this->simades->get_hubungan()) {
            $data['hubungan'] = $this->simades->get_hubungan();
        } else {
            $data['hubungan'] = array();
        }
        if ($data['warga'] = $this->simades->get_warga()) {
            $data['warga'] = $this->simades->get_warga();
        } else {
            $data['warga'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/kk_add', $data);
        $this->load->view('template/foot');
    }

    function kk_add() {
        $this->form_validation->set_rules('kk', 'required');
        $this->form_validation->set_rules('nik', 'required');
        $this->form_validation->set_rules('nama', 'required');
        $this->form_validation->set_rules('tmpt', 'required');
        $this->form_validation->set_rules('tgl', 'required');
        $this->form_validation->set_rules('sex', 'required');
        $this->form_validation->set_rules('darah', 'required');
        $this->form_validation->set_rules('agama', 'required');
        $this->form_validation->set_rules('pndk', 'required');
        $this->form_validation->set_rules('pkrj', 'required');
        $this->form_validation->set_rules('cct', 'required');
        $this->form_validation->set_rules('kwn', 'required');
        $this->form_validation->set_rules('hub', 'required');
        $this->form_validation->set_rules('nik_ib', 'required|no');
        $this->form_validation->set_rules('nm_ib', 'required');
        $this->form_validation->set_rules('nik_ay', 'required|no');
        $this->form_validation->set_rules('nm_ay', 'required');
        $this->form_validation->set_rules('anak', 'required');
        $this->form_validation->set_rules('rt', 'required');
        $this->form_validation->set_rules('rw', 'required');
        $this->form_validation->set_rules('sts_pndk', 'required');
        $this->form_validation->set_rules('srt_lhr', 'required');
        $this->form_validation->set_rules('warga', 'required');
        $this->form_validation->set_rules('ketpen', 'required|no');
        $this->form_validation->set_rules('foto', 'required|no');
        $this->form_validation->set_rules('almt_lm', 'required|no');
        $this->form_validation->set_rules('almt_br', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('petugas/kk');
        } else {
            $nik['kk_id'] = $this->input->post('kk');
            $nik['nik'] = $this->input->post('nik');
            $nik['nama'] = $this->input->post('nama');
            $nik['tempat_lahir'] = $this->input->post('tmpt');
            $nik['tgl_lahir'] = $this->input->post('tgl');
            $nik['jns_klmn_id'] = $this->input->post('sex');
            $nik['gol_darah_id'] = $this->input->post('darah');
            $nik['agama_id'] = $this->input->post('agama');
            $nik['pendidikan_id'] = $this->input->post('pndkn');
            $nik['pekerjaan_id'] = $this->input->post('pkrjn');
            $nik['cacat_id'] = $this->input->post('cct');
            $nik['perkawinan_id'] = $this->input->post('kwn');
            $nik['hubungan_id'] = $this->input->post('hub');
            $nik['nik_ibu'] = $this->input->post('nik_ib');
            $nik['nama_ibu'] = $this->input->post('nm_ib');
            $nik['nik_ayah'] = $this->input->post('nik_ay');
            $nik['nama_ayah'] = $this->input->post('nm_ay');
            $nik['anak_ke'] = $this->input->post('anak');
            $nik['rt'] = $this->input->post('rt');
            $nik['rw'] = $this->input->post('rw');
            $nik['alamat_sblm'] = $this->input->post('almt_lm');
            $nik['alamat'] = $this->input->post('almt_br');
            $nik['status_penduduk'] = $this->input->post('sts_pndk');
            $nik['no_surat_kelahiran'] = $this->input->post('srt_lhr');
            $nik['kewarganegaraan_id'] = $this->input->post('warga');
            $nik['ket_pendidikan'] = $this->input->post('ketpen');
            $nik['username'] = $this->session->userdata('username');
            $kk['id'] = $this->input->post('kk');
            $kk['nik'] = $this->input->post('nik');
            $kk['kelurahan_id'] = 1;
            $kk['kecamatan_id'] = 24;
            $kk['kabupaten_id'] = 10;
            $kk['propinsi_id'] = 33;
            $this->simades->kk_add($kk);
            $this->simades->nik_add($nik);
            redirect('petugas/kk');
        }
    }

    function kk_baru() {
        $data['title'] = 'Tambah Kartu Keluarga';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['sex'] = $this->simades->get_sex()) {
            $data['sex'] = $this->simades->get_sex();
        } else {
            $data['sex'] = array();
        }
        if ($data['darah'] = $this->simades->get_darah()) {
            $data['darah'] = $this->simades->get_darah();
        } else {
            $data['darah'] = array();
        }
        if ($data['agama'] = $this->simades->get_agama()) {
            $data['agama'] = $this->simades->get_agama();
        } else {
            $data['agama'] = array();
        }
        if ($data['skul'] = $this->simades->get_pendidikan()) {
            $data['skul'] = $this->simades->get_pendidikan();
        } else {
            $data['skul'] = array();
        }
        if ($data['gawe'] = $this->simades->get_pekerjaan()) {
            $data['gawe'] = $this->simades->get_pekerjaan();
        } else {
            $data['gawe'] = array();
        }
        if ($data['cacat'] = $this->simades->get_cacat()) {
            $data['cacat'] = $this->simades->get_cacat();
        } else {
            $data['cacat'] = array();
        }
        if ($data['perkawinan'] = $this->simades->get_perkawinan()) {
            $data['perkawinan'] = $this->simades->get_perkawinan();
        } else {
            $data['perkawinan'] = array();
        }
        if ($data['hubungan'] = $this->simades->get_hubungan()) {
            $data['hubungan'] = $this->simades->get_hubungan();
        } else {
            $data['hubungan'] = array();
        }
        if ($data['warga'] = $this->simades->get_warga()) {
            $data['warga'] = $this->simades->get_warga();
        } else {
            $data['warga'] = array();
        }
        if ($data['dusun'] = $this->simades->get_dusun()) {
            $data['dusun'] = $this->simades->get_dusun();
        } else {
            $data['dusun'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/kk_baru', $data);
        $this->load->view('template/foot');
    }

    function kk_new() {
        $this->form_validation->set_rules('nama', 'required');
        $this->form_validation->set_rules('tmpt', 'required');
        $this->form_validation->set_rules('tgl', 'required');
        $this->form_validation->set_rules('sex', 'required');
        $this->form_validation->set_rules('darah', 'required');
        $this->form_validation->set_rules('agama', 'required');
        $this->form_validation->set_rules('pndk', 'required');
        $this->form_validation->set_rules('pkrj', 'required');
        $this->form_validation->set_rules('cct', 'required');
        $this->form_validation->set_rules('kwn', 'required');
        $this->form_validation->set_rules('hub', 'required');
        $this->form_validation->set_rules('nik_ib', 'required|no');
        $this->form_validation->set_rules('nm_ib', 'required');
        $this->form_validation->set_rules('nik_ay', 'required|no');
        $this->form_validation->set_rules('nm_ay', 'required');
        $this->form_validation->set_rules('anak', 'required');
        $this->form_validation->set_rules('rt', 'required');
        $this->form_validation->set_rules('rw', 'required');
        $this->form_validation->set_rules('sts_pndk', 'required');
        $this->form_validation->set_rules('srt_lhr', 'required');
        $this->form_validation->set_rules('warga', 'required');
        $this->form_validation->set_rules('ketpen', 'required|no');
        $this->form_validation->set_rules('foto', 'required|no');
        $this->form_validation->set_rules('almt_lm', 'required|no');
        $this->form_validation->set_rules('almt_br', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('petugas/kk');
        } else {
            $wkt = date('dmy'); //mendapatkan waktu sekarang dengan fungsi php
            $pro_id = $this->simades->get_id_propinsi();
            $kab_id = $this->simades->get_id_kabupaten();
            $kec_id = $this->simades->get_id_kecamatan();
            $now = date('Y-m-d');
            $di = $this->simades->get_last_kk_id($now);
            $id = $di[0]->id;
            $last = substr($id, 12, 4); //memecah hasil dari database untuk mendapatkan id terakhir
            $next = $last + 1; //id terakhir ditambah 1
            $new = sprintf('%04s', $next);
            $lok = $pro_id[0]->id . $kab_id[0]->id . $kec_id[0]->id; //mengabungkan variabel untuk menyusun kode lokasi
            $kk_id = $lok . $wkt . $new; // id kk yang akan diinsert ke dalam database

            $tmp = $this->input->post('tgl'); //menangkap tanggal lahir yang kemudian ditampung di variabel tmp
            $bln = substr($tmp, 0, 2); //di substring untuk mendapatkan bulan
            $tgl = substr($tmp, 3, 2);
            $thn = substr($tmp, 6, 4);
            $tgl_lahir = $thn . $bln . $tgl;

            $yr = substr($tmp, 8, 2);
            $dbt = $thn . '-' . $bln . '-' . $tgl;
            $sex = $this->input->post('sex');
            if ($sex == 2) {
                $date = $tgl + 40; //jika jenis kelaminnya perempuan, maka tanggalnya ditambah 40.
            } else {
                $date = $tgl; //jika jenis kelaminnya laki-laki, maka tanggalnya tetap.
            }
            $last_nik = $this->simades->get_last_nik($dbt);
            $sub = $last_nik[0]->nik;
            $nik_last = substr($sub, 12, 4);
            $next_nik = $nik_last + 1; //id terakhir ditambah 1
            $nik_new = sprintf('%04s', $next_nik);
            $new_nik = $lok . $date . $bln . $yr . $nik_new;

            $kk['id'] = $kk_id;
            $kk['nik'] = $new_nik;
            $kk['rt_id'] = $this->input->post('rt');
            $kk['rw_id'] = $this->input->post('rw');
            $kk['dusun_id'] = $this->input->post('almt_br');
            $kk['kelurahan_id'] = 1;
            $kk['kecamatan_id'] = $kec_id[0]->id;
            $kk['kabupaten_id'] = $kab_id[0]->id;
            $kk['propinsi_id'] = $pro_id[0]->id;
            $kk['dibuat'] = date('Y-m-d');
            $kk['username'] = $this->session->userdata('username');
            echo '<pre>';
            print_r($kk);
            echo '</pre>';
            $this->simades->kk_add($kk);

            $nik['nik'] = $new_nik;
            $nik['kk_id'] = $kk_id;
            $nik['nama'] = strtoupper($this->input->post('nama'));
            $nik['tempat_lahir'] = strtoupper($this->input->post('tmpt'));
            $nik['tgl_lahir'] = $tgl_lahir;
            $nik['jns_klmn_id'] = $this->input->post('sex');
            $nik['gol_darah_id'] = $this->input->post('darah');
            $nik['agama_id'] = $this->input->post('agama');
            $nik['pendidikan_id'] = $this->input->post('pndkn');
            $nik['pekerjaan_id'] = $this->input->post('pkrjn');
            $nik['cacat_id'] = $this->input->post('cct');
            $nik['perkawinan_id'] = $this->input->post('kwn');
            $nik['hubungan_id'] = $this->input->post('hub');
            $nik['nik_ibu'] = $this->input->post('nik_ib');
            $nik['nama_ibu'] = $this->input->post('nm_ib');
            $nik['nik_ayah'] = $this->input->post('nik_ay');
            $nik['nama_ayah'] = $this->input->post('nm_ay');
            $nik['anak_ke'] = $this->input->post('anak');
            $nik['alamat_sblm'] = strtoupper($this->input->post('almt_lm'));
            $nik['status_penduduk'] = $this->input->post('sts_pndk');
            $nik['no_surat_kelahiran'] = $this->input->post('srt_lhr');
            $nik['kewarganegaraan_id'] = $this->input->post('warga');
            $nik['ket_pendidikan'] = $this->input->post('ketpen');
            $nik['dibuat'] = date('Y-m-d');
            $nik['username'] = $this->session->userdata('username');
            $cek = $this->simades->cek_nik($new_nik); // cek apakah nik sudah ada?
            if ($cek == 0) {
                $this->simades->nik_add($nik); // jika nik tidak ada, maka akan dimasukkan kedalam database
            } else {
                $this->simades->del_last_nik($new_nik);
            }
            redirect('petugas/kk');
        }
    }

    function kk_del() {
        $id = $this->uri->segment(3);
        $this->simades->kk_del($id);
        redirect('petugas/kk');
    }

    function penduduk() {
        $data['title'] = 'Penduduk';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['pndk'] = $this->simades->get_pndk()) {
            $data['pndk'] = $this->simades->get_pndk();
        } else {
            $data['pndk'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/penduduk', $data);
        $this->load->view('template/foot');
    }

    function nik() {
        $data['title'] = 'Tambah Penduduk';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['sex'] = $this->simades->get_sex()) {
            $data['sex'] = $this->simades->get_sex();
        } else {
            $data['sex'] = array();
        }
        if ($data['darah'] = $this->simades->get_darah()) {
            $data['darah'] = $this->simades->get_darah();
        } else {
            $data['darah'] = array();
        }
        if ($data['agama'] = $this->simades->get_agama()) {
            $data['agama'] = $this->simades->get_agama();
        } else {
            $data['agama'] = array();
        }
        if ($data['skul'] = $this->simades->get_pendidikan()) {
            $data['skul'] = $this->simades->get_pendidikan();
        } else {
            $data['skul'] = array();
        }
        if ($data['gawe'] = $this->simades->get_pekerjaan()) {
            $data['gawe'] = $this->simades->get_pekerjaan();
        } else {
            $data['gawe'] = array();
        }
        if ($data['cacat'] = $this->simades->get_cacat()) {
            $data['cacat'] = $this->simades->get_cacat();
        } else {
            $data['cacat'] = array();
        }
        if ($data['perkawinan'] = $this->simades->get_perkawinan()) {
            $data['perkawinan'] = $this->simades->get_perkawinan();
        } else {
            $data['perkawinan'] = array();
        }
        if ($data['hubungan'] = $this->simades->get_hubungan()) {
            $data['hubungan'] = $this->simades->get_hubungan();
        } else {
            $data['hubungan'] = array();
        }
        if ($data['warga'] = $this->simades->get_warga()) {
            $data['warga'] = $this->simades->get_warga();
        } else {
            $data['warga'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/nik_add', $data);
        $this->load->view('template/foot');
    }

    function nik_add() {
        $this->form_validation->set_rules('kk', 'required');
        $this->form_validation->set_rules('nik', 'required');
        $this->form_validation->set_rules('nama', 'required');
        $this->form_validation->set_rules('tmpt', 'required');
        $this->form_validation->set_rules('tgl', 'required');
        $this->form_validation->set_rules('sex', 'required');
        $this->form_validation->set_rules('darah', 'required');
        $this->form_validation->set_rules('agama', 'required');
        $this->form_validation->set_rules('pndk', 'required');
        $this->form_validation->set_rules('pkrj', 'required');
        $this->form_validation->set_rules('cct', 'required');
        $this->form_validation->set_rules('kwn', 'required');
        $this->form_validation->set_rules('hub', 'required');
        $this->form_validation->set_rules('nik_ib', 'required|no');
        $this->form_validation->set_rules('nm_ib', 'required');
        $this->form_validation->set_rules('nik_ay', 'required|no');
        $this->form_validation->set_rules('nm_ay', 'required');
        $this->form_validation->set_rules('anak', 'required');
        $this->form_validation->set_rules('rt', 'required');
        $this->form_validation->set_rules('rw', 'required');
        $this->form_validation->set_rules('sts_pndk', 'required');
        $this->form_validation->set_rules('srt_lhr', 'required');
        $this->form_validation->set_rules('warga', 'required');
        $this->form_validation->set_rules('ketpen', 'required|no');
        $this->form_validation->set_rules('foto', 'required|no');
        $this->form_validation->set_rules('almt_lm', 'required|no');
        $this->form_validation->set_rules('almt_br', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('petugas/nik');
        } else {
            $nik['kk_id'] = $this->input->post('kk');
            $nik['nik'] = $this->input->post('nik');
            $nik['nama'] = strtoupper($this->input->post('nama'));
            $nik['tempat_lahir'] = strtoupper($this->input->post('tmpt'));
            $nik['tgl_lahir'] = $this->input->post('tgl');
            $nik['jns_klmn_id'] = $this->input->post('sex');
            $nik['gol_darah_id'] = $this->input->post('darah');
            $nik['agama_id'] = $this->input->post('agama');
            $nik['pendidikan_id'] = $this->input->post('pndkn');
            $nik['pekerjaan_id'] = $this->input->post('pkrjn');
            $nik['cacat_id'] = $this->input->post('cct');
            $nik['perkawinan_id'] = $this->input->post('kwn');
            $nik['hubungan_id'] = $this->input->post('hub');
            $nik['nik_ibu'] = $this->input->post('nik_ib');
            $nik['nama_ibu'] = strtoupper($this->input->post('nm_ib'));
            $nik['nik_ayah'] = $this->input->post('nik_ay');
            $nik['nama_ayah'] = strtoupper($this->input->post('nm_ay'));
            $nik['anak_ke'] = $this->input->post('anak');
            $nik['rt'] = $this->input->post('rt');
            $nik['rw'] = $this->input->post('rw');
            $nik['alamat_sblm'] = strtoupper($this->input->post('almt_lm'));
            $nik['alamat'] = strtoupper($this->input->post('almt_br'));
            $nik['status_penduduk'] = $this->input->post('sts_pndk');
            $nik['no_surat_kelahiran'] = $this->input->post('srt_lhr');
            $nik['kewarganegaraan_id'] = $this->input->post('warga');
            $nik['ket_pendidikan'] = $this->input->post('ketpen');
            $nik['username'] = $this->session->userdata('username');
            $this->simades->nik_add($nik);
            redirect('petugas/penduduk');
        }
    }

    function nik_baru() {
        $data['title'] = 'Tambah Penduduk Baru';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        if ($data['sex'] = $this->simades->get_sex()) {
            $data['sex'] = $this->simades->get_sex();
        } else {
            $data['sex'] = array();
        }
        if ($data['darah'] = $this->simades->get_darah()) {
            $data['darah'] = $this->simades->get_darah();
        } else {
            $data['darah'] = array();
        }
        if ($data['agama'] = $this->simades->get_agama()) {
            $data['agama'] = $this->simades->get_agama();
        } else {
            $data['agama'] = array();
        }
        if ($data['skul'] = $this->simades->get_pendidikan()) {
            $data['skul'] = $this->simades->get_pendidikan();
        } else {
            $data['skul'] = array();
        }
        if ($data['gawe'] = $this->simades->get_pekerjaan()) {
            $data['gawe'] = $this->simades->get_pekerjaan();
        } else {
            $data['gawe'] = array();
        }
        if ($data['cacat'] = $this->simades->get_cacat()) {
            $data['cacat'] = $this->simades->get_cacat();
        } else {
            $data['cacat'] = array();
        }
        if ($data['perkawinan'] = $this->simades->get_perkawinan()) {
            $data['perkawinan'] = $this->simades->get_perkawinan();
        } else {
            $data['perkawinan'] = array();
        }
        if ($data['hubungan'] = $this->simades->get_hubungan()) {
            $data['hubungan'] = $this->simades->get_hubungan();
        } else {
            $data['hubungan'] = array();
        }
        if ($data['warga'] = $this->simades->get_warga()) {
            $data['warga'] = $this->simades->get_warga();
        } else {
            $data['warga'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('petugas/menu');
        $this->load->view('petugas/nik_baru', $data);
        $this->load->view('template/foot');
    }

    function nik_new() {
        $this->form_validation->set_rules('kk', 'required');
        $this->form_validation->set_rules('nama', 'required');
        $this->form_validation->set_rules('tmpt', 'required');
        $this->form_validation->set_rules('tgl', 'required');
        $this->form_validation->set_rules('sex', 'required');
        $this->form_validation->set_rules('darah', 'required');
        $this->form_validation->set_rules('agama', 'required');
        $this->form_validation->set_rules('pndk', 'required');
        $this->form_validation->set_rules('pkrj', 'required');
        $this->form_validation->set_rules('cct', 'required');
        $this->form_validation->set_rules('kwn', 'required');
        $this->form_validation->set_rules('hub', 'required');
        $this->form_validation->set_rules('nik_ib', 'required|no');
        $this->form_validation->set_rules('nm_ib', 'required');
        $this->form_validation->set_rules('nik_ay', 'required|no');
        $this->form_validation->set_rules('nm_ay', 'required');
        $this->form_validation->set_rules('anak', 'required');
        $this->form_validation->set_rules('sts_pndk', 'required');
        $this->form_validation->set_rules('srt_lhr', 'required');
        $this->form_validation->set_rules('warga', 'required');
        $this->form_validation->set_rules('ketpen', 'required|no');
        $this->form_validation->set_rules('foto', 'required|no');
        $this->form_validation->set_rules('almt_lm', 'required|no');
        if ($this->form_validation->run() == FALSE) {
            redirect('petugas/nik_baru');
        } else {
            $pro_id = $this->simades->get_id_propinsi();
            $kab_id = $this->simades->get_id_kabupaten();
            $kec_id = $this->simades->get_id_kecamatan();
            $lok = $pro_id[0]->id . $kab_id[0]->id . $kec_id[0]->id; //mengabungkan variabel untuk menyusun kode lokasi

            $tmp = $this->input->post('tgl'); //menangkap tanggal lahir yang kemudian ditampung di variabel tmp
            $bln = substr($tmp, 0, 2); //di substring untuk mendapatkan bulan
            $tgl = substr($tmp, 3, 2);
            $thn = substr($tmp, 6, 4);
            $tgl_lahir = $thn . $bln . $tgl;

            $yr = substr($tmp, 8, 2);
            $dbt = $thn . '-' . $bln . '-' . $tgl;
            $sex = $this->input->post('sex');
            if ($sex == 2) {
                $date = $tgl + 40; //jika jenis kelaminnya perempuan, maka tanggalnya ditambah 40.
            } else {
                $date = $tgl; //jika jenis kelaminnya laki-laki, maka tanggalnya tetap.
            }
            $last_nik = $this->simades->get_last_nik($dbt);
            $sub = $last_nik[0]->nik;
            $nik_last = substr($sub, 12, 4);
            $next_nik = $nik_last + 1; //id terakhir ditambah 1
            $nik_new = sprintf('%04s', $next_nik);
            $new_nik = $lok . $date . $bln . $yr . $nik_new;


            $nik['nik'] = $new_nik;
            $nik['kk_id'] = $this->input->post('kk');
            $nik['nama'] = strtoupper($this->input->post('nama'));
            $nik['tempat_lahir'] = strtoupper($this->input->post('tmpt'));
            $nik['tgl_lahir'] = $tgl_lahir;
            $nik['jns_klmn_id'] = $this->input->post('sex');
            $nik['gol_darah_id'] = $this->input->post('darah');
            $nik['agama_id'] = $this->input->post('agama');
            $nik['pendidikan_id'] = $this->input->post('pndkn');
            $nik['pekerjaan_id'] = $this->input->post('pkrjn');
            $nik['cacat_id'] = $this->input->post('cct');
            $nik['perkawinan_id'] = $this->input->post('kwn');
            $nik['hubungan_id'] = $this->input->post('hub');
            $nik['nik_ibu'] = $this->input->post('nik_ib');
            $nik['nama_ibu'] = $this->input->post('nm_ib');
            $nik['nik_ayah'] = $this->input->post('nik_ay');
            $nik['nama_ayah'] = $this->input->post('nm_ay');
            $nik['anak_ke'] = $this->input->post('anak');
            $nik['alamat_sblm'] = strtoupper($this->input->post('almt_lm'));
            $nik['status_penduduk'] = $this->input->post('sts_pndk');
            $nik['no_surat_kelahiran'] = $this->input->post('srt_lhr');
            $nik['kewarganegaraan_id'] = $this->input->post('warga');
            $nik['ket_pendidikan'] = $this->input->post('ketpen');
            $nik['dibuat'] = date('Y-m-d');
            $nik['username'] = $this->session->userdata('username');
            $cek = $this->simades->cek_nik($new_nik); // cek apakah nik sudah ada?
            if ($cek == 0) {
                $this->simades->nik_add($nik); // jika nik tidak ada, maka akan dimasukkan kedalam database
            } else {
                $this->simades->del_last_nik($new_nik);
            }
            redirect('petugas/penduduk');
        }
    }

    function result() {
        $this->form_validation->set_rules('search', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect('petugas/penduduk');
        } else {
            $data['title'] = 'Hasil';
            $data['username'] = $this->session->userdata('username');
            $id = $this->input->post('search');
            if ($data['isi'] = $this->simades->get_sys()) {
                $data['isi'] = $this->simades->get_sys();
            } else {
                $data['isi'] = array();
            }
            if ($data['result'] = $this->simades->cari($id)) {
                $data['result'] = $this->simades->cari($id);
            } else {
                $data['result'] = array();
            }
            $this->load->view('template/head', $data);
            $this->load->view('petugas/menu');
            $this->load->view('petugas/result', $data);
            $this->load->view('template/foot');
        }
    }

    function del_nik() {
        $nik = $this->uri->segment(3);
        $this->simades->del_nik($nik);
        redirect('petugas/penduduk');
    }

}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */
?>